package chatGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import signals.SignalTooBigException;

/* 
 * @author Vansh and deepali

 */
public class ConnectionWindow extends JFrame implements ActionListener {

    protected DefaultListModel usersListModel;
    protected JList usersList;
    protected JButton logIn;
    protected JTextField usernameBox;
    protected JTextArea chatBox;
    protected Calendar cal;
    protected JScrollPane chatPane, inputPane, listPane, userInputPane, userChatPane;
    protected JPanel window;
    protected JLabel loginLabel, listLabel, infoLabel;
    protected JTextArea inputArea;
    protected JFrame frame;
    protected JLabel labUsername;
    protected ChatGui cg;
    protected String formattedUsername;
    private ChatWindow cw;

    /**
     * This class allows use open the chat window
     *
     */
    public ConnectionWindow(ChatGui cg) {
        super();
        this.cg = cg;
        this.display();
    }

    public void display() {

        /**
         * set the Jframe and JButtons
         */
        frame = new JFrame("Chat System");
        JPanel southPanel = new JPanel();

        frame.getContentPane().add(BorderLayout.NORTH, southPanel);
        southPanel.setBackground(Color.gray);
        southPanel.setLayout(new GridBagLayout());

        usernameBox = new JTextField(30);

        logIn = new JButton("Connect");

        Font font = new Font("Arial", Font.BOLD, 15);

        GridBagConstraints left = new GridBagConstraints();
        left.anchor = GridBagConstraints.WEST;
        GridBagConstraints right = new GridBagConstraints();
        right.anchor = GridBagConstraints.EAST;
        right.weightx = 2.0;
        labUsername = new JLabel("Username : ");
        southPanel.add(labUsername);
        southPanel.add(usernameBox);

        southPanel.add(logIn);

        logIn.addActionListener(this);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(670, 500);
    }

    /**
     * Action performed when connect button is pressed.
     *
     * @param event
     */

    public void actionPerformed(ActionEvent event) {
        if (usernameBox.getText().length() < 1) {
            // do nothing 
        } else {
            if (!"".equals(usernameBox.getText())) {
                try {
                    System.out.println("connection of " + usernameBox.getText());
                    formattedUsername = usernameBox.getText().trim();
                    System.out.println("Say Hello to Everyone (ConnectionWindow)");
                    cg.connect(formattedUsername);

                } catch (SignalTooBigException ex) {
                    Logger.getLogger(ConnectionWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            frame.setVisible(false);

        }
    }

    public ChatWindow getCw() {
        return cw;
    }

    public void setCw(ChatWindow cw) {
        this.cw = cw;
    }

}
